package com.rongke.web.api;

import com.alibaba.fastjson.JSONObject;
import com.rongke.commons.JsonResp;
import com.rongke.web.ud.UDAPI;
import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/facePhoto")
@Transactional
@CrossOrigin
public class FacePhotoController {
    private Logger log = Logger.getLogger(this.getClass());

    /**
     * @param
     * @return 返回值JsonResp
     * @人脸对比认证
     */
    @RequestMapping(value = "/getPhotoPass", method = RequestMethod.GET)
    public JsonResp getPhotoPass(String session_id, String facePhoto, String param) throws IOException {
        log.debug("人脸对比认证");
        String url = facePhoto+"&param="+param;
        log.info("sessionId==========="+url);
        JSONObject jsonObject = UDAPI.faceCompare(session_id,url);
        log.info("+++++++++++++++++"+jsonObject);
        Map map = (Map)jsonObject.get("result");
        String result = map.get("success").toString();
        if(result.equals("true")){
            Map data = (Map)jsonObject.get("data");
            String similarity = data.get("similarity").toString();
            Object thresholds = data.get("thresholds");
            JSONObject thresholds1 = JSONObject.parseObject(JSONObject.toJSONString(thresholds));
            if (Double.valueOf(similarity)>Double.valueOf(thresholds1.get("1e-4").toString())){
                return JsonResp.ok();
            }else{
                return JsonResp.fa("您的人脸匹配度不够高！");
            }
        }else{
            return JsonResp.fa(map.get("message").toString());
        }
    }
}
